//
//  LWNetWorkTools.m
//  1.28  weibo
//
//  Created by mac on 15/2/28.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWNetWorkTools.h"
#import "LWAccount.h"
#import "LWstatuses.h"
#import "LWUser.h"
#import "MJExtension.h"
#import "LWUnReadCount.h"
#import "LWStatuseSendRequest.h"
#import "LWStatuseRequset.h"
#import "LWStatuseResult.h"

#define LWWeiboBaseUrl [NSURL URLWithString:@"https://api.weibo.com/"]
#define LWLoadUserInfoUrlString @"2/users/show.json"
#define LWLoadUnreadCountUrlString @"2/remind/unread_count.json"
#define LWSendMessageWithIcon @"2/statuses/upload.json"
#define LWSendMessageWithOutIcon @"2/statuses/update.json"
#define LWLoadHomeStatuse @"2/statuses/home_timeline.json"

#define LWAuthorizeUrl @"https://api.weibo.com/oauth2/authorize"
#define LWClientId @"3110138268"
#define LWRedirectUri @"http://www.baidu.com"
#define LWAccessToken @"oauth2/access_token"
#define LWClient_secret @"6bcddc2aabf782740435a5201a7eff4b"

@implementation LWNetWorkTools
/**
 *  网络工具类的单例
 */
+ (instancetype)shareNetWorkTools{

    static LWNetWorkTools *tools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[LWNetWorkTools alloc] initWithBaseURL:LWWeiboBaseUrl sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        
        //让AFN支持以下的类型
        tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain" ,@"application/json", @"text/json", @"text/javascript", nil];
    });
    return tools;
}
/**
 *  加载用户的信息
 */
- (void)loadUserInfo:(successBluok)success failure:(failureBlock)failure{

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    LWAccount *account = [LWAccount accountFromSandBox];
    parameters[@"uid"] = account.uid;
    parameters[@"access_token"] = account.access_token;
    //2. 发送post请求
    [self GET:LWLoadUserInfoUrlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            //如果成功将服务器返回的字典转化为模型
            LWUser *user = [LWUser objectWithKeyValues:responseObject];
            success(user);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)loadUnreadCount:(void (^)(LWUnReadCount *))success failure:(failureBlock)failure{

    // 2.封装请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"access_token"] = [LWAccount accountFromSandBox].access_token;
    parameters[@"uid"] = [LWAccount accountFromSandBox].uid;
    // 3.发送请求
    [self GET:LWLoadUnreadCountUrlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            LWUnReadCount *unReadCount = [LWUnReadCount objectWithKeyValues:responseObject];
            success(unReadCount);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)loadAccessTokenWithCode:(NSString *)code success:(void (^)(LWAccount *))success failure:(failureBlock)failure{

    //1.封装参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"client_id"] = LWClientId;
    parameters[@"client_secret"] = LWClient_secret;
    parameters[@"grant_type"] = @"authorization_code";
    parameters[@"code"] = code;
    parameters[@"redirect_uri"] = LWRedirectUri;

    [self POST:LWAccessToken parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            LWAccount *account = [LWAccount objectWithKeyValues:responseObject];
            [account save];
            success(account);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
      
        if (failure) {
            failure(error);
        }
    }];
}

- (void)sendMessageWithParameters:(LWStatuseSendRequest *)parameters success:(void (^)(LWstatuses *))success failure:(failureBlock)failure{
    
   
    if (parameters.image != nil) {
        
        UIImage *image = parameters.image;
        parameters.image = nil;
        [self POST:LWSendMessageWithIcon parameters:parameters.keyValues constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            NSData *iamgeData = UIImagePNGRepresentation(image);
            [formData appendPartWithFileData:iamgeData name:@"pic" fileName:@"bossli" mimeType:@"image/png"];
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            if (success) {
                LWstatuses *statuse = [LWstatuses objectWithKeyValues:responseObject];
                success(statuse);
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
    }else{
        
        [self POST:LWSendMessageWithOutIcon parameters:parameters.keyValues success:^(NSURLSessionDataTask *task, id responseObject) {
            if (success) {
                LWstatuses *statuse = [LWstatuses objectWithKeyValues:responseObject];
                success(statuse);
            }
                
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            if (failure) {
                failure(error);
            }
        }];
    }
}

- (void)loadHomeStatusesWithParameters:(LWStatuseRequset *)parameters success:(void (^)(LWStatuseResult *))success failure:(failureBlock)failure{

    [self GET:LWLoadHomeStatuse parameters:parameters.keyValues success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success) {
            LWStatuseResult *result = [LWStatuseResult objectWithKeyValues:responseObject];
            success(result);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
